package com.grupoconstruobras.sigo.data.sequencegenerator;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;

public class IdSolicitudDiligenciaGenerator implements IdentifierGenerator{

	@Override
	public Serializable generate(SessionImplementor session, Object object)
			throws HibernateException {
		  Connection connection = session.connection();
	        try {

	            PreparedStatement ps = connection
	                    .prepareStatement("select exhumaciones.getnextidsolicitudcaso() as idCaso");

	            ResultSet rs = ps.executeQuery();
	            if (rs.next()) {
	                Long id = new Long(rs.getInt("idCaso"));
	              
	                return id;
	            }

	        } catch (SQLException e) {
	           
	            throw new HibernateException(
	                    "Unable to generate Stock Code Sequence");
	       
	    }
		return null;
	}

}
